<template>
  <div>
    <AssignEditorDialog ref="assignEditorDialog" :taskItemList="filteredTaskItemList" :taskId="taskId"/>
    <el-row class="bottom-space" style="line-height: unset" ref="task-item-list-row">
      <el-col :span="8">
        <el-button v-if="taskId !== undefined" size="small" type="primary" @click="insertTaskItem">新增子项目</el-button>
        <el-button v-if="taskId !== undefined" size="small" type="primary" @click="bulkImport">批量导入</el-button>
        <el-button v-if="taskId !== undefined && knowledgeLibId==='0001AA100000000WD6YX'" size="small" type="primary"
                   @click="uploadClick">word导入
        </el-button>
        <el-button v-if="taskId !== undefined && knowledgeLibId==='0001AA100000000WD6YX'" size="small" type="primary"
                   @click="importResults">word导入结果
        </el-button>

        <div v-if="taskId !== undefined && knowledgeLibId==='0001AA100000000WD6YX'"
             style="display: inline-block;margin-left: 10px">

        </div>
      </el-col>
      <el-col :span="2">
        <el-button size="small" @click="assignEditor">分配编辑</el-button>
      </el-col>
      <el-col :span="2">
        <el-button size="small" @click="assignFirstAuditor">分配初审</el-button>
      </el-col>
      <el-col v-show="auditLevel_bak > 1" :span="2">
        <el-button size="small" @click="assign_second_auditor">分配复审</el-button>
      </el-col>
      <el-col v-show="auditLevel_bak > 2" :span="2">
        <el-button size="small" @click="assign_third_auditor">分配终审</el-button>
      </el-col>
      <el-col :span="8" style="float: right">
        <Search ref="search" :audit-level="auditLevel_bak" :knowledgeLibId="knowledgeLibId"
                @search='filterTaskItemList'></Search>
      </el-col>
      <el-dialog :visible.sync="dialogVisible" title="选择对应的问答库任务" width="500px">

        <el-row>
          <el-col :span="6">
            <span class="label">选择任务<em style="color: red">*</em></span>
          </el-col>
          <el-col :span="18">
            <el-select v-model="selectedTaskId" filterable placeholder="请选择"
                       style="width: 100%">
              <el-option
                v-for="(item,index) in taskList"
                :key="index"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-col>
        </el-row>
        <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取 消</el-button>
           <div style="display: inline-block;margin-left: 10px">
           <el-upload ref="upload"
                      :action="'/pmph/action/knowledge/wordHtml?taskId=' + taskId + '&knowledgeLibId='+ knowledgeLibId+'&QuestionAnswerTaskId='+ selectedTaskId "
                      :hidden="UTIL.isEmpty(selectedTaskId)"
                      :multiple="true"
                      :on-progress="uploadProgress"
                      :on-remove="removeFile"
                      :on-success="updateWordsTable"
                      :show-file-list="false"
                      accept=".docx"
                      auto-upload class="upload-demo">
            <el-button size="small" type="primary"> 确定导入</el-button>
          </el-upload>
           </div>
      </span>
      </el-dialog>
    </el-row>
    <TaskItemTable ref="taskItemTable"
                   :auditLevel="auditLevel_bak"
                   :knowledgeLibId="knowledgeLibId"
                   :taskItemList="filteredTaskItemList"
                   :systemType="systemType"
                   :height="height - 50"
                   class="bottom-space" @insertTaskItem="insertTaskItemCommit" @deleteTaskItem="deleteTaskItem"/>
  </div>
</template>
<script>
import AssignEditorDialog from "./AssignEditorDialog"
import Search from "./Search"
import TaskItemTable from "./TaskItemTable"
import {Loading} from 'element-ui';

export default {
  name: 'TaskItemList',
  components: {AssignEditorDialog, Search, TaskItemTable},
  props: ['auditLevel_bak', 'taskId', 'knowledgeLibId', 'taskItemList', 'systemType', 'height'],
  data() {
    return {
      uploadVisible: false,
      filteredTaskItemList: this.taskItemList,
      tableHeight: 0,
      failList: [],
      dialogVisible: false,
      selectedTaskId: '',
      taskList: [],
      elLoadingComponent: '',
    }
  },
  methods: {
    closeDialog() {
      this.dialogVisible = false
      this.selectedTaskId = ""
    },
    uploadProgress() {
      this.dialogVisible = false
      this.elLoadingComponent = Loading.service({fullscreen: true});

    },
    uploadClick() {
      this.axios.get('/pmph/action/task/selectQuestionAnswerTaskId', {
        params: {}
      }).then((response) => {
        if (response.data.length > 0) {
          this.taskList = response.data
          this.dialogVisible = true
        } else {
          this.$message.warning("当前知识库没有正在进行中的任务, 请先新建任务!")
        }
      })
    },
    importResults() {
      this.$router.push({
        path: '/wordResult',
        query: {
          taskId: this.taskId
        }
      })
    },
    removeFile() {
      this.$refs.uplaod.clearFiles()
    },
    updateWordsTable(response, file, fileList) {
      let failList = [];
      let successList = [];
      fileList.filter(item => {
        if (item.response === false) {
          failList.push(item.name)
        } else {
          successList.push(item.name)
        }
      })
      if (fileList.filter(item => item.status === 'ready').length === 0) {
        let name = failList.join("','");
        if (failList.length > 0) {
          this.$confirm("您成功导入稿件“" + successList.length + "”个，失败“" + failList.length + "”个,是否查看导入结果", '提示', {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'error'
          }).then(() => {
            this.$router.push({
              path: '/wordResult',
              query: {
                taskId: this.taskId
              }
            })
          })
          //this.$message.error(name + "解析失败")
        } else {
          this.$message.success("您成功导入稿件“" + successList.length + "”个")
        }
        this.$refs.upload.clearFiles()
        this.$nextTick(() => { // 以服务的方式调用的 Loading 需要异步关闭
          this.elLoadingComponent.close();
        });
      }
      this.axios.get("/pmph/action/task/selectTaskItem", {
        params: {
          taskId: this.taskId
        }
      }).then((response) => {
        this.taskItemList = response.data
        console.log(response.data)
      })
      this.exportVisible = false
      this.dialogVisible = false
    },
    bulkImport() {
      this.$router.push({
        path: '/task/subTaskItem', query: {id: this.knowledgeLibId, taskId: this.taskId}
      })
    },
    insertTaskItem() {
      let tempTaskItem = {
        taskId: this.taskId,
        knowledgeId: '',
        name: '',
        type: 0,
        editor: '',
        wordsCount: 0,
        firstAuditor: '',
        secondAuditor: '',
        thirdAuditor: '',
        status: 0,
        remark: '',
      }
      this.$refs.taskItemTable.insertTaskItem(tempTaskItem)
    },
    deleteTaskItem(taskItem) {
      this.filteredTaskItemList.remove(taskItem)
      this.taskItemList.remove(taskItem)
    },
    insertTaskItemCommit(taskItem) {
      this.taskItemList.unshift(taskItem)
    },
    assignEditor() {
      if (this.filteredTaskItemList.length === 0) {
        this.$message.warning("当前过滤条件下子任务为空, 不能分配")
        return
      }
      this.$refs.assignEditorDialog.title = '分配编辑'
      this.$refs.assignEditorDialog.visible = true
    },
    assignFirstAuditor() {
      if (this.filteredTaskItemList.length === 0) {
        this.$message.warning("当前过滤条件下子任务为空, 不能分配")
        return
      }
      this.$refs.assignEditorDialog.title = '分配初审'
      this.$refs.assignEditorDialog.visible = true
    },
    assign_second_auditor() {
      if (this.filteredTaskItemList.length === 0) {
        this.$message.warning("当前过滤条件下子任务为空, 不能分配")
        return
      }
      this.$refs.assignEditorDialog.title = '分配复审'
      this.$refs.assignEditorDialog.visible = true
    },
    assign_third_auditor() {
      if (this.filteredTaskItemList.length === 0) {
        this.$message.warning("当前过滤条件下子任务为空, 不能分配")
        return
      }
      this.$refs.assignEditorDialog.title = '分配终审'
      this.$refs.assignEditorDialog.visible = true
    },
    filterTaskItemList(name, editor, firstAuditor, secondAuditor, thirdAuditor, remark, type, status,
                       nameSuggestionFilterFlag, relationDisease) {
      let tempTaskItemList = this.taskItemList
      if (name) {
        tempTaskItemList = tempTaskItemList.filter(taskItem => taskItem.name.search(name) !== -1)
      }
      if (editor) {
        if (editor === 'null') {
          tempTaskItemList = tempTaskItemList.filter(taskItem => !taskItem.editor)
        } else {
          tempTaskItemList = tempTaskItemList.filter(taskItem => taskItem.editor === editor)
        }
      }
      if (firstAuditor) {
        if (firstAuditor === 'null') {
          tempTaskItemList = tempTaskItemList.filter(taskItem => !taskItem.firstAuditor)
        } else {
          tempTaskItemList = tempTaskItemList.filter(taskItem => taskItem.firstAuditor === firstAuditor)
        }
      }
      if (secondAuditor) {
        if (secondAuditor === 'null') {
          tempTaskItemList = tempTaskItemList.filter(taskItem => !taskItem.secondAuditor)
        } else {
          tempTaskItemList = tempTaskItemList.filter(taskItem => taskItem.secondAuditor === secondAuditor)
        }
      }
      if (thirdAuditor) {
        if (thirdAuditor === 'null') {
          tempTaskItemList = tempTaskItemList.filter(taskItem => !taskItem.thirdAuditor)
        } else {
          tempTaskItemList = tempTaskItemList.filter(taskItem => taskItem.thirdAuditor === thirdAuditor)
        }
      }
      if (remark) {
        tempTaskItemList = tempTaskItemList.filter(taskItem => taskItem.remark.search(remark) !== -1)
      }
      if (relationDisease) {
        tempTaskItemList = tempTaskItemList.filter(taskItem => taskItem.relationDisease.search(relationDisease) !== -1)
      }
      if (type) {
        tempTaskItemList = tempTaskItemList.filter(taskItem => taskItem.type === parseInt(type))
      }
      if (status || status === 0) {
        tempTaskItemList = tempTaskItemList.filter(taskItem => taskItem.status === status)
      }
      if (nameSuggestionFilterFlag) {
        tempTaskItemList = tempTaskItemList.filter(taskItem => UTIL.hasUnconfirmedNameSuggestion(taskItem.nameSuggestion))
      }
      this.filteredTaskItemList = tempTaskItemList
    },
  },
  watch: {
    'taskItemList': {
      handler() {
        this.$refs.search.confirm()
      },
      deep: true
    }
  }
}
</script>

<style>
.bottom-space {
  margin-bottom: 10px;
}
</style>
